Fix duplicate organizations in selection prompt - #24
Closed
JoshSalway wants to merge 1 commit into
Closed
Conversation
When authenticating multiple times, the same API tokens were appended to config.json without deduplication. Each duplicate token triggered a separate API call returning the same organization, causing the org selection prompt to show duplicate entries. Add unique() to both apiTokens() (to handle existing duplicated configs) and addApiToken() (to prevent future duplicates). Fixes #22 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This comment was marked as spam.
This comment was marked as spam.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes duplicate organizations appearing in the CLI org selection prompt when a user has authenticated multiple times.
Fixes #22
Root Cause Analysis
ConfigRepository::addApiToken()used->push($token)without any deduplication. When a user runscloud authmultiple times (common when tokens expire), the same token gets appended toconfig.jsonrepeatedly:Then in
HasAClient::resolveApiToken(), when there are multiple tokens, each token triggers a separate API call to$client->meta()->organization()to fetch the org name for the selection prompt. With 5 duplicate tokens, the same org is fetched 5 times and displayed 5 times in the picker.How the bug was found and verified
We traced the flow from the org selection prompt back through
HasAClient::resolveApiToken()→ConfigRepository::addApiToken(). Simulating 5 auth sessions with the same token against the original code produces:{ "api_tokens": [ "755|example-token-abc123", "755|example-token-abc123", "755|example-token-abc123", "755|example-token-abc123", "755|example-token-abc123" ] }Token count: 5, Unique token count: 1 — each duplicate triggers a separate API call to fetch the org, so the picker shows the same organization 5 times.
After the fix, the same 5 auth sessions produce:
{ "api_tokens": [ "755|example-token-abc123" ] }Token count: 1 — org picker shows 1 entry as expected.
All 31 existing tests pass with this change (0 failures, 32 assertions).
Changes
apiTokens()now returns->unique()->values()— deduplicates on read (fixes existing bloated config files immediately)addApiToken()now chains->unique()->values()afterpush()— prevents future duplicates from being writtenTest plan
cloud authmultiple times and verify~/.config/cloud/config.jsondoes not accumulate duplicate tokenscloud environment:variables) and verify each organization appears only onceconfig.jsonand verify they are deduplicated at read time (5 identical tokens → deduplicated to 1 → org appears once in picker)./vendor/bin/pest→ 31 passed, 32 assertions🤖 Generated with Claude Code